/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package main;

/**************************************************************************************
 * Author(s): Leszek Karcz
 * Main class that initializes the game and all its data and components
 **************************************************************************************/
public class Clock extends Thread//implements Runnable
{
	private int tickLength;
	private int tick;
	private int msCount;
	private boolean running=false;
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Clock constructor
	 **************************************************************************************/
	public Clock()
	{ 
		this(1000);
	}	
	public Clock(int tickLength)
	{
		if(tickLength<=0)
		{
			this.tickLength=1000;
		}
		else
		{
			this.tickLength=tickLength;			
		}
		this.tickLength=tickLength;
		this.reset();
		this.start();
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Clock loop
	 **************************************************************************************/
	public void run()
	{
		while(true)
		{
			if(this.running)
			{
				this.msCount++;
			}
			delay(1);
		}
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Start clock
	 **************************************************************************************/
	public void startClock()
	{
		this.running=true;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Start clock
	 **************************************************************************************/
	public void stopClock()
	{
		this.running=false;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * check if the clock is running
	 **************************************************************************************/
	public boolean isRunning()
	{
		return this.running;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Gat the current tick
	 **************************************************************************************/
	public int getTick()
	{
		return this.tick;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Reset clock to time 0
	 **************************************************************************************/
	public void reset()
	{
		this.tick=0;
		this.msCount=0;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Get the current time
	 **************************************************************************************/
	public String getTime()
	{
		int s,m,h;
		int time=this.msCount/1000;
		s=time % 60;
		m=((time-s)/60) % 60;
		h=(time-s)/60 - m;
		return h+":"+((m+"").length()==1 ? "0" : "")+m+":"+((s+"").length()==1 ? "0" : "")+s;
	}

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Check if time tick match next execution tick
	 **************************************************************************************/
	public boolean isNext()
	{
		if((int)(this.msCount/this.tickLength)>this.tick)
		{
			this.tick++;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * delays the execution by certain interval in microseconds
	 **************************************************************************************/
	private static void delay(int interval)
	{
		try
		{
			Thread.sleep(interval);
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}	
}
